package com.yy.xshell.process;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wxy
 * @version 1.0
 * @date 2024/8/20 17:15
 */
public class HandlerPipeline<T> {

    private Handler<T> head, tail;

    private final Map<String, Handler<T>> name2ctx = new HashMap<String, Handler<T>>(4);


    public void addLast(String name , Handler<T> commandHandler){
        if(head == null){
            head = commandHandler;
            tail = commandHandler;
        }else {
            tail.next = commandHandler;
            tail = commandHandler;
            name2ctx.put(name, commandHandler);
        }

    }

    public void addLast(Handler<T> commandHandler){
        addLast(commandHandler.getClass().getSimpleName(), commandHandler);
    }

    public T handler(T command){
        if(this.head == null){
            return command;
        }
        return head.invoke(command);
    }

}
